import { RegisterComponent } from "@feng3d/ecs";
import { Points } from "../../thirdparty/three/imports";
import { BufferGeometry3D } from "../core/BufferGeometry3D";
import { Component3D } from "../core/Component3D";
import { Material3D } from "../materials/Material3D";

declare module "@feng3d/ecs"
{
    interface ComponentMap { Points3D: Points3D; }
}

/**
 * 包装`three`中`Points`
 */
@RegisterComponent({ name: 'Points3D' })
export class Points3D extends Component3D
{
    private _points: Points;

    get geometry()
    {
        return this._geometry;
    }
    set geometry(v)
    {
        this._geometry = v;
        if (this._points)
        {
            this._points.geometry = this._geometry?._geometry;
        }
    }
    private _geometry: BufferGeometry3D;

    get material()
    {
        return this._material;
    }
    set material(v)
    {
        this._material = v;
        if (this._points)
        {
            this._points.material = this._material?._material;
        }
    }
    private _material: Material3D;

    init()
    {
        super.init();

        this._points = new Points(this.geometry?._geometry, this.material?._material);

        this._entity._group.add(this._points);
        this._entity.isPoints = true;
    }

    destroy(): void
    {
        this._entity._group.remove(this._points);
        this._points = null;
        this._entity.isPoints = false;

        super.destroy();
    }
}
